package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/requests"
	"goravel/app/models"
	"goravel/app/services/sheet"
)

type ServiceController struct {
	//Dependent services
	sheet.SheetService
}

func NewServiceController() *ServiceController {
	return &ServiceController{
		//Inject services
	}
}

// Index 用户分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ServiceController
// @Accept       json
// @Produce      json
// @Id ServiceIndex
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param  name  query  string  false  "name"
// @Param  pageSize  query  string  false  "pageSize"
// @Param  currentPage  query  string  false  "currentPage"
// @Param  sort  query  string  false  "sort"
// @Param  order  query  string  false  "order"
// @Success 200 {string} json {}
// @Router       /api/admin/service [get]
func (r *ServiceController) Index(ctx http.Context) http.Response {
	services := []models.Service{}
	queries := ctx.Request().Queries()
	res, err := httpfacade.NewResult(ctx).SearchByParams(queries, nil).ResultPagination(&services)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return res
}

func (r *ServiceController) Show(ctx http.Context) http.Response {
	return nil
}

// Store
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ServiceController
// @Accept       json
// @Produce      json
// @Id ServiceStore
// @Security ApiKeyAuth
// @Param userData body requests.ServiceRequest true "用户数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/service [post]
func (r *ServiceController) Store(ctx http.Context) http.Response {
	var ServiceRequest requests.ServiceRequest
	errors, err := ctx.Request().ValidateRequest(&ServiceRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	facades.Orm().Query().Model(&models.Service{}).Create(&models.Service{
		Name: ServiceRequest.Name,
		Code: ServiceRequest.Code,
	})
	return httpfacade.NewResult(ctx).Success("创建成功", nil)
}

// Update
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ServiceController
// @Accept       json
// @Produce      json
// @Id ServiceUpdate
// @Security ApiKeyAuth
// @Param userData body requests.ServiceRequest true "用户数据"
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/service/{id} [put]
func (r *ServiceController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	Service := models.Service{}
	facades.Orm().Query().Model(&models.Service{}).Where("id=?", id).Find(&Service)
	var ServiceRequest requests.ServiceRequest
	errors, err := ctx.Request().ValidateRequest(&ServiceRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	Service.Name = ServiceRequest.Name
	Service.Code = ServiceRequest.Code
	facades.Orm().Query().Model(&models.Service{}).Where("id=?", id).Save(&Service)
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

// Destroy
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ServiceController
// @Accept       json
// @Produce      json
// @Id ServiceDestroy
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/admin/service/{id} [delete]
func (r *ServiceController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	service := models.Service{}
	facades.Orm().Query().Model(&models.Service{}).Where("id=?", id).Delete(&service, id)
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}

// Option  后代
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ServiceController
// @Accept       json
// @Produce      json
// @Id ServiceOption
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param serviceData body interface{}  true "关键词"
// @Success 200 {string} json {}
// @Router       /api/admin/service/option [post]
func (r *ServiceController) Option(ctx http.Context) http.Response {
	name := ctx.Request().Input("name")
	services := []models.Service{}
	facades.Orm().Query().Model(&models.Service{}).Where("name like ?", "%"+name+"%").Find(&services)
	return httpfacade.NewResult(ctx).Success("", services)
}

// Init  初始化
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         ServiceController
// @Accept       json
// @Produce      json
// @Id ServiceInit
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Router       /api/admin/service/init [get]
func (r *ServiceController) Init(ctx http.Context) http.Response {
	r.FillService("service.xlsx")
	return httpfacade.NewResult(ctx).Success("初始化成功", nil)
}
